package com.nidu.demo.file.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.file.convertor.FileConvertor;
import com.nidu.demo.file.gateway.FileGateway;
import com.nidu.demo.file.mapper.FileMapper;
import com.nidu.demo.file.mapper.dataobject.FileDO;
import com.nidu.demo.file.model.File;
import com.nidu.demo.file.model.FileCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 文件网关实现
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class FileGatewayImpl implements FileGateway {

    private final FileMapper mapper;
    private final FileConvertor convertor;

    @Override
    public File create(File file) {
        FileDO fileDO = convertor.toDataObject(file);
        mapper.insert(fileDO);
        return convertor.toDomainObject(fileDO);
    }

    @Override
    public File update(File file) {
        FileDO fileDO = convertor.toDataObject(file);
        mapper.updateById(fileDO);
        return convertor.toDomainObject(fileDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public File getById(Long id) {
        FileDO fileDO = mapper.selectById(id);
        if (fileDO == null) {
            return null;
        }
        return convertor.toDomainObject(fileDO);
    }

    @Override
    public List<File> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<FileDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FileDO::getId, ids);
        List<FileDO> fileDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(fileDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(fileDOS);
    }

    @Override
    public List<File> listByCondition(FileCriteria criteria) {
        LambdaQueryWrapper<FileDO> wrapper = buildQueryWrapper(criteria);
        List<FileDO> fileDOS = mapper.selectList(wrapper);
        if (CollUtil.isEmpty(fileDOS)) {
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(fileDOS);
    }

    @Override
    public PageResponse<File> pageByCondition(FileCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<FileDO> wrapper = buildQueryWrapper(criteria);

        // 创建分页对象
        Page<FileDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }

        // 执行分页查询
        mapper.selectPage(page, wrapper);
        List<File> files = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(files, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    private LambdaQueryWrapper<FileDO> buildQueryWrapper(FileCriteria criteria) {
        LambdaQueryWrapper<FileDO> wrapper = new LambdaQueryWrapper<>();
        
        // 租户条件
        wrapper.eq(criteria.getTenantId() != null, FileDO::getTenantId, criteria.getTenantId());
        
        // 其他查询条件
        wrapper.eq(criteria.getConfigId() != null, FileDO::getConfigId, criteria.getConfigId());
        wrapper.like(!StrUtil.isEmpty(criteria.getName()), FileDO::getName, criteria.getName());
        wrapper.like(!StrUtil.isEmpty(criteria.getType()), FileDO::getType, criteria.getType());
        
        return wrapper;
    }

}
